<template>
    <div>
        <div class="selectwarehouseclass">
            <div class="selectwarehouseclass_item" ref='father'>
                <div class="select_app" v-for="item in selectdata">
                    <el-image class="select-item-logo" :src="item.avatar?item.avatar:'https://img2.baidu.com/it/u=2277413437,2773342262&fm=253&fmt=auto&app=120&f=JPEG?w=516&h=500'">></el-image>
                    <span style="margin-left: 5px;font-size: 12px">{{item.userName}}</span>
                </div>
            </div>
            <el-select placeholder="请选择成员" :style="{height:selectheight+'px'}" style="width:100%;" v-model="selectwarehouse" ref="select" @visible-change="visibleChange" size="medium" @focus="membwefocus">
                <template slot="empty">
                    <div class="select">
                        <div class="select-search">
                            <el-input size="small" placeholder="请输入内容" style="height:25px" prefix-icon="el-icon-search" v-model="queryInput" @input="termUserlistclck"></el-input>
                        </div>
                        <div v-for="(group,groupIndex) in options">
                            <div class="select-header" @click="groupClick(group)">
                                <div class="select-header-left">
                                    <span class="select-header-title">{{group.termName}}</span>
                                    <i class="task-header-actions-icon el-icon-refresh" v-if="inputLoading == true && group.type == true" :class="[inputLoading == true?'refreshRe':'']"></i>
                                    <span class="select-header-number" v-else>({{group.userList.length}})</span>
                                </div>
                                <i class="iconfont icon-xia" :class="[group.type==true?'select-header-icon':'select-header-iconTwo']"></i>
                            </div>
                            <!--多选-->
                            <template v-if="group.type == true">
                                <div :class="[item.type == true?'select-item-choose':'select-item']" v-for="(item,index) in group.userList" @click="groupItemClick(item,index,groupIndex)">
                                    <el-image class="select-item-logo" :src="item.avatar?item.avatar:'https://img2.baidu.com/it/u=2277413437,2773342262&fm=253&fmt=auto&app=120&f=JPEG?w=516&h=500'"></el-image>
                                    <span class="select-item-title">{{item.userName}}</span>
                                </div>
                            </template>
                            <!-- 单选-->
                        </div>
                        <el-empty description="暂无数据" :image-size="40" v-if="options.length == 0"></el-empty>
                    </div>
                </template>
            </el-select>
        </div>

    </div>
</template>

<script>
import { getTreeData } from "@/api/termuser";
import utils from "@/utils/utils";
export default {
    props: ["memberList", "parentProjId"],
    data() {
        return {
            options: [],
            queryInput: "",
            inputLoading: false,
            itemIndex: -1,
            groupIndexs: -1,
            selectdata: [],
            selectwarehouse: "",
            selectheight: 36,
            projId: "",
        };
    },

    created() {
        this.projId = this.parentProjId;
    },
    watch: {
        parentProjId(newval, oldval) {
            if (this.personprojId == newval) {
                return;
            }
            this.projId = newval;
            this.selectdata = [];
            // this.$emit("seclectmemberclear");
            // this.memberList=[]
            this.listlick();
            // this.$emit("selectsuccess", this.selectdata);
            // this.options.forEach((item) => {
            //   item.userList.forEach((data) => {
            //     data.type=true
            //   });
            // });
        },
        memberList: {
            handler(newName, oldName) {
                console.log(newName)
                console.log('chengyuan')
                this.memberList = newName;
                this.optionchan();
            },
            immediate: true,
            deep: true,
        },
        selectdata: {
            handler(newName, oldName) {
                this.heightshow();
            },
            // immediate: true,
            deep: true,
        },
        options: {
            handler(newName, oldName) {
                if (!utils.isNull(this.memberList)) {
                    this.optionchan();
                }
            },
            // immediate: true,
            deep: true,
        },
    },
    mounted() {
        setTimeout(() => {
            this.listlick();
        }, 1000);
    },
    methods: {

        membwefocus() {},
        async termUserlistclck() {
            // let params = new URLSearchParams();
            // params.append("userName", this.queryInput);
            // params.append(" projId", this.projId);
            let res = await getTreeData({userName:this.queryInput,projId:this.projId});
            res.data.forEach((item) => {
                this.$set(item, "type", true);
                item.userList.forEach((data) => {
                    this.$set(item, "data", false);
                });
            });
            this.options = res.data;
        },
        optionchan() {
            var _this = this
            if (!utils.isNull(_this.memberList)) {
              _this.memberList.forEach((data) => {
                _this.options.forEach((v, index) => {
                        v.userList.forEach((item, indexs) => {
                            if (data == item.userId) {

                              _this.selectdata.push(item);
                              _this.selectdata = _this.unique(_this.selectdata);
                                item.type = true;
                              _this.$nextTick(() => {
                                _this.heightshow();
                                });
                            }
                        });
                    });
                });
            }
        },
        unique(arr) {
            const res = new Map();
            return arr.filter(
                (arr) => !res.has(arr.userId) && res.set(arr.userId, 1)
            );
        },
        async listlick() {
            let res = await getTreeData(
                { current: 1, size: 20, projId: this.projId },
                false
            );
            res.data.forEach((item) => {
                this.$set(item, "type", true);
                item.userList.forEach((data) => {
                    this.$set(item, "data", false);
                });
            });
            this.options = res.data;
        },
        heightshow() {
            this.selectheight = this.$refs.father.offsetHeight+10;
            if (this.selectheight < 36) {
                this.selectheight = 36;
            }
        },

        emptyclick() {
            var _this = this;
            _this.options.forEach(function (item) {
                item.type = false;
                item.userList.forEach(function (items) {
                    items.type = false;
                });
            });
            _this.selectdata = [];
            _this.$emit("selectsuccess", this.selectdata);
        },
        groupClick(item) {
            item.type = !item.type;
        },
        groupItemClick(item, index, groupIndex) {
            console.log(item);
            var _this = this;
            item.type = !item.type;
            this.options.forEach((evevt) => {
                evevt.userList.forEach((element) => {
                    if (item.userId == element.userId) {
                        element.type = item.type;
                    }
                });
            });
            var data = [];
            _this.options.forEach(function (item) {
                if (item.type == true) {
                    item.userList.forEach(function (items) {
                        if (items.type == true) {
                            data.push(items);
                        }
                    });
                }
            });
            this.selectdata = this.unique(data);
        },
        visibleChange(item) {
            var _this = this;
            if (item == false) {
                var data = [];
                _this.options.forEach(function (item) {
                    if (item.type == true) {
                        item.userList.forEach(function (items) {
                            if (items.type == true) {
                                data.push(items);
                            }
                        });
                    }
                });
                this.selectdata = data;
                this.selectdata = this.unique(this.selectdata);
                let selectdataarray = [];
                this.selectdata.forEach((v) => {
                    selectdataarray.push(v.userId);
                });
                let selectdataString = selectdataarray.join(",");
                this.$emit("selectsuccess", selectdataString);
            }
        },
        inputClick(data) {
            var _this = this;
            if (this.inputLoading == true) {
                return;
            }
            this.inputLoading = true;
            setTimeout(() => {
                _this.inputLoading = false;
            }, 1000);
        },
    },
};
</script>

<style scoped>
.select {
    max-height: 380px;
    overflow: auto;
}

.select-search {
    padding-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
    box-sizing: border-box;
}
.select-header {
    padding: 10px 10px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #f5f6f8;
    flex: 1;
}

.select-header-title {
    flex: 1;
    font-size: 14px;
    font-weight: bold;
    color: #333333;
    margin-right: 10px;
    margin-left: 4px;
}

.select-header-icon {
    font-size: 12px;
    color: #333333;
    margin-right: 4px;
    transition: all 0.5s;
    transform: rotate(-180deg);
}

.select-header-iconTwo {
    font-size: 12px;
    color: #333333;
    margin-right: 4px;
    transition: all 0.5s;
}

.select-item {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 10px 10px;
    cursor: pointer;
    color: #333333;
}

.select-item-choose {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 10px 10px;
    cursor: pointer;
    background-color: rgba(194, 214, 255, 0.22);
    color: #2c7ef8;
}

.select-item:hover {
    background-color: rgba(88, 96, 105, 0.06);
}

.select-item-logo {
    width: 20px;
    height: 20px;
    border-radius: 50%;
}

.select-item-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
    margin-left: 10px;
    display: block;
    max-width: 220px;
}

.select-header-left {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.select-header-number {
    font-size: 14px;
    color: #333333;
    font-weight: bold;
}

.task-header-actions-icon {
    border-radius: 4px;
    cursor: pointer;
    color: #909aaa;
    font-size: 14px;
}

.refreshRe {
    -webkit-animation: rotate 1.2s linear infinite; /*Safari and Chrome*/
}
@keyframes rotate {
    0% {
        transform: rotate(360deg);
        -ms-transform: rotate(360deg); /* IE 9 */
        -moz-transform: rotate(360deg); /* Firefox */
        -o-transform: rotate(360deg);
    }
    100% {
        transform: rotate(0deg);
        -ms-transform: rotate(0deg); /* IE 9 */
        -moz-transform: rotate(0deg); /* Firefox */
        -o-transform: rotate(0deg);
    }
}

@-webkit-keyframes rotate {
    0% {
        -moz-transform: rotate(360deg);
    }
    100% {
        -moz-transform: rotate(0deg);
    }
}
.appcontner {
    position: relative;
}
.retorn {
    width: 100%;
    height: 50%;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    pointer-events: none;
    font-size: 14px;
    /* font-weight: 700; */
    color: rgb(46, 64, 94);
    display: flex;
    align-items: center;
    justify-content: center;
}
.select-header-iconTwo {
    font-size: 12px;
    color: #333333;
}
.selectwarehouseclass {
    position: relative;
    margin-bottom: 15px;
}
.selectwarehouseclass_item {
    position: absolute;
    z-index: 2;
    top: 5px;
    left: 10px;
    display: flex;
    flex-wrap: wrap;
    pointer-events: none;
    right:30px;
    background-color: white;
}
.select_app {
    cursor: pointer;
    background-color: #fff;
    display: flex;
    align-items: center;
    margin-right: 10px;
    height: 25px;
}
/deep/.el-input .el-input__inner {
    height: 100%;
}
/deep/.el-input--medium {
    height: 100%;
}
/deep/.el-input__suffix-inner{
  display: flex;
  align-items: center;
}
</style>
